home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)Z / (A)Z7.ADF / C / random / rnd.s < prev   
Text File  |  1986-10-31  |  3KB  |  100 lines

  1.  
  2.         XDEF    RandomSeed,Random    assembler entry points
  3.         XDEF    _RandomSeed,_Random    C entry points
  4.         CODE
  5. ;=============================================================================
  6. ; NAME
  7. ;    RandomSeed - seed random number generator, call once at beginning of
  8. ;          your program.  CurrentTime provides useful values for this
  9. ;
  10. ; SYSNOPSIS
  11. ;    RandomSeed( SeedValue )
  12. ;            D0
  13. ;
  14. ; FUNCTION
  15. ;    Seeds the random number generator
  16. ;
  17. ; INPUTS
  18. ;    SeedValue - a longword containing any value you like
  19. ;
  20. ; RESULT
  21. ;    Random number generator is initialised
  22. ;
  23. ; BUGS/LIMITATIONS
  24. ;    None that I know of
  25. ;
  26. ; SEE ALSO
  27. ;
  28. ;============================================================================
  29.  
  30. _RandomSeed    MOVE.L    4(SP),D0    entry point for C functions
  31. RandomSeed    ADD.L    D0,D1        user seed in d0 (d1 too)
  32.         MOVEM.L    D0/D1,RND
  33.  
  34. ; drops through to the main random function (not user callable)
  35.  
  36. LongRnd        MOVEM.L    D2-D3,-(SP)    
  37.         MOVEM.L    RND,D0/D1    D0=LSB's, D1=MSB's of random number
  38.         ANDI.B    #$0E,D0        ensure upper 59 bits are an...
  39.         ORI.B    #$20,D0        ...odd binary number
  40.         MOVE.L    D0,D2
  41.         MOVE.L    D1,D3
  42.         ADD.L    D2,D2        accounts for 1 of 17 left shifts
  43.         ADDX.L    D3,D3        [D2/D3] = RND*2
  44.         ADD.L    D2,D0
  45.         ADDX.L    D3,D1        [D0/D1] = RND*3
  46.         SWAP    D3        shift [D2/D3] additional 16 times
  47.         SWAP    D2
  48.         MOVE.W    D2,D3
  49.         CLR.W    D2
  50.         ADD.L    D2,D0        add to [D0/D1]
  51.         ADDX.L    D3,D1
  52.         MOVEM.L    D0/D1,RND    save for next time through
  53.         MOVE.L    D1,D0        most random part to D0
  54.         MOVEM.L    (SP)+,D2-D3
  55.         RTS
  56.  
  57. ;=============================================================================
  58. ; NAME
  59. ;    Random - returns a random integer in the specified range
  60. ;
  61. ; SYSNOPSIS
  62. ;    RndNum = Random( UpperLimit )
  63. ;      D0          D0
  64. ;
  65. ; FUNCTION
  66. ;    returns a random integer in the range 0 to UpperLimit-1
  67. ;
  68. ; INPUTS
  69. ;     UpperLimit - a long(or short will do) in the range 1-65535
  70. ;
  71. ; RESULT
  72. ;    a random integer is returned to you, real quick!
  73. ;
  74. ; BUGS/LIMITATIONS
  75. ;    range was limited to 1-65535 to avoid problems with the DIVU instruction
  76. ;    which can return real wierd values if the result is larger than 16 bits.
  77. ;
  78. ; SEE ALSO
  79. ;
  80. ;============================================================================
  81.  
  82. _Random        MOVE.L    4(SP),D0    C entry point
  83. Random        MOVE.W    D2,-(SP)
  84.         MOVE.W    D0,D2        save upper limit
  85.         BEQ.S    10$        range of 0 returns 0 always
  86.         BSR.S    LongRnd        get a longword random number
  87.         CLR.W    D0        use upper word (it's most random)
  88.         SWAP    D0
  89.         DIVU.W    D2,D0        divide by range...
  90.         CLR.W    D0        ...and use remainder for the value
  91.         SWAP    D0        result in D0.W
  92. 10$        MOVE.W    (SP)+,D2
  93.         RTS
  94.  
  95.         BSS
  96. RND        DS.L    2        random number
  97.         END
  98.  
  99.  
  100.